import {uniConfig} from '@/config/index'
class LocalCache {
    // 添加
    setCache(key: string, value: any) {
      uni.setStorage({
        key: key,
        data: value
      })
    }
    // 查找
    async getCache(key: string,defaultData:any) {
      // obj=>sting=>obj
      return new Promise(async (resolve, reject) => {
        uni.getStorage({
          key: key,
          success: function (res) {
            resolve(res.data)
          },
          fail: function (res) {
            reject()
          },
        })
      })
    }
    // 删除
    deleteCache(key: string) {
      uni.removeStorage({
        key: key
      });
    }
    // 清理
    clearCache() {
      uni.clearStorage();
    }
    // 程序登录
    miniAppFetchApiCheckLogin(){
      return new Promise(async (resolve, reject) => {
        uni.login({
          success: async loginCodeInfo => {
            await uni.request({
              url: uniConfig.prodPath + '/wxmini/open/get',
              method: 'POST',
              data: {
                code:loginCodeInfo.code
              },
              header: {
                'Content-Type': 'application/x-www-form-urlencoded',
                appid: uniConfig.appId
              },
              success: async sucInfo => {
                const memberInfo =  sucInfo.data.data
                uni.setStorage({
                  key: 'memberInfo',
                  data: memberInfo
                })
                resolve(memberInfo)
              },
            })
          },
          fail: () => {
            uni.hideLoading()
          }
        })
      })
    }
  }
  
  export default new LocalCache()